﻿/**********************************************************************
 * DYNAJAX Project [v1.06] - code by Benoit BEGUIN
 * first release: october 2007
 * last update: october 2008
 * Paris, France
 * 
 * DynAjax web site: http://www.dynajax.org/
 * DynAjax samples web site: http://samples.dynajax.org/
 * DynAjax download: http://download.dynajax.org/
 * Any question: mailto:admin@dynajax.org
 * Company support: http://www.odesys.fr
 **********************************************************************/

using System;
using System.Data;
using System.Configuration;
using System.Web;

/// <summary>
/// Session manager - methods that will be called to manage session, current user etc.
/// (login, logout, checks...)
/// 
/// This is an example file for DynAjax template web application
/// </summary>
public class SessionManager {
    #region Constants
    public const string RESULT_OK = "ok";
    public const string RESULT_KO = "ko";
    #endregion

    #region Constructors
    public SessionManager() { }
    #endregion

    #region Static methods
    /// <summary>
    /// Log in user : return OK or KO according to user/password couple check
    /// 
    /// for this example, it returns OK if login = password
    /// </summary>
    public static string loginUser(string login, string password) {
        try {
            System.Threading.Thread.Sleep(1000);
            if (login != null && password != null) {
                if (!login.Trim().Equals("") && login.ToLower().Trim().Equals(password.ToLower().Trim())) {
                    HttpContext.Current.Session["crtUser"] = login;
                    return RESULT_OK;
                }
            }
        }
        catch { }

        return RESULT_KO;
    }

    /// <summary>
    /// Logout current user and return OK
    /// If current user cannot be logged out, return KO
    /// </summary>
    /// <returns></returns>
    public static string logoutCurrentUser() {
        try {
            HttpContext.Current.Session["crtUser"] = null;
            return RESULT_OK;
        }
        catch {
            return RESULT_KO;
        }
    }

    /// <summary>
    /// Check authentication: current user is logged ?
    /// current user is active ?
    /// </summary>
    public static bool checkAuthentication() {
        return (HttpContext.Current.Session["crtUser"] != null);
    }

    /// <summary>
    /// Return logged user info (Session["crtUser"] contains userLogin)
    /// </summary>
    public static string getCurrentUserLogin() {
        return (string)HttpContext.Current.Session["crtUser"];
    }
    #endregion
}
